
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cleverhans.attacks.projected_gradient_descent &#8212; CleverHans  documentation</title>
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cleverhans.attacks.projected_gradient_descent</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">The ProjectedGradientDescent attack.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>

<span class="kn">from</span> <span class="nn">cleverhans.attacks.attack</span> <span class="kn">import</span> <span class="n">Attack</span>
<span class="kn">from</span> <span class="nn">cleverhans.attacks.fast_gradient_method</span> <span class="kn">import</span> <span class="n">FastGradientMethod</span>
<span class="kn">from</span> <span class="nn">cleverhans</span> <span class="kn">import</span> <span class="n">utils_tf</span>
<span class="kn">from</span> <span class="nn">cleverhans.compat</span> <span class="kn">import</span> <span class="n">softmax_cross_entropy_with_logits</span>
<span class="kn">from</span> <span class="nn">cleverhans.utils_tf</span> <span class="kn">import</span> <span class="n">clip_eta</span><span class="p">,</span> <span class="n">random_lp_vector</span>


<div class="viewcode-block" id="ProjectedGradientDescent"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ProjectedGradientDescent">[docs]</a><span class="k">class</span> <span class="nc">ProjectedGradientDescent</span><span class="p">(</span><span class="n">Attack</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  This class implements either the Basic Iterative Method</span>
<span class="sd">  (Kurakin et al. 2016) when rand_init is set to 0. or the</span>
<span class="sd">  Madry et al. (2017) method when rand_minmax is larger than 0.</span>
<span class="sd">  Paper link (Kurakin et al. 2016): https://arxiv.org/pdf/1607.02533.pdf</span>
<span class="sd">  Paper link (Madry et al. 2017): https://arxiv.org/pdf/1706.06083.pdf</span>

<span class="sd">  :param model: cleverhans.model.Model</span>
<span class="sd">  :param sess: optional tf.Session</span>
<span class="sd">  :param dtypestr: dtype of the data</span>
<span class="sd">  :param default_rand_init: whether to use random initialization by default</span>
<span class="sd">  :param kwargs: passed through to super constructor</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="n">FGM_CLASS</span> <span class="o">=</span> <span class="n">FastGradientMethod</span>

  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtypestr</span><span class="o">=</span><span class="s1">&#39;float32&#39;</span><span class="p">,</span>
               <span class="n">default_rand_init</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a ProjectedGradientDescent instance.</span>
<span class="sd">    Note: the model parameter should be an instance of the</span>
<span class="sd">    cleverhans.model.Model abstraction provided by CleverHans.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nb">super</span><span class="p">(</span><span class="n">ProjectedGradientDescent</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="o">=</span><span class="n">sess</span><span class="p">,</span>
                                                   <span class="n">dtypestr</span><span class="o">=</span><span class="n">dtypestr</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">feedable_kwargs</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;eps&#39;</span><span class="p">,</span> <span class="s1">&#39;eps_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;y_target&#39;</span><span class="p">,</span> <span class="s1">&#39;clip_min&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;clip_max&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">structural_kwargs</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ord&#39;</span><span class="p">,</span> <span class="s1">&#39;nb_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;rand_init&#39;</span><span class="p">,</span> <span class="s1">&#39;clip_grad&#39;</span><span class="p">,</span>
                              <span class="s1">&#39;sanity_checks&#39;</span><span class="p">,</span> <span class="s1">&#39;loss_fn&#39;</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">default_rand_init</span> <span class="o">=</span> <span class="n">default_rand_init</span>

<div class="viewcode-block" id="ProjectedGradientDescent.generate"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ProjectedGradientDescent.generate">[docs]</a>  <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate symbolic graph for adversarial examples and return.</span>

<span class="sd">    :param x: The model&#39;s symbolic inputs.</span>
<span class="sd">    :param kwargs: See `parse_params`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Parse and save attack-specific parameters</span>
    <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_params</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="n">asserts</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="c1"># If a data range was specified, check that the input was in that range</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">asserts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">utils_tf</span><span class="o">.</span><span class="n">assert_greater_equal</span><span class="p">(</span><span class="n">x</span><span class="p">,</span>
                                                   <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span>
                                                           <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)))</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">asserts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">utils_tf</span><span class="o">.</span><span class="n">assert_less_equal</span><span class="p">(</span><span class="n">x</span><span class="p">,</span>
                                                <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">,</span>
                                                        <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)))</span>

    <span class="c1"># Initialize loop variables</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rand_init</span><span class="p">:</span>
      <span class="n">eta</span> <span class="o">=</span> <span class="n">random_lp_vector</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span>
                             <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rand_init_eps</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">),</span>
                             <span class="n">dtype</span><span class="o">=</span><span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">eta</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>

    <span class="c1"># Clip eta</span>
    <span class="n">eta</span> <span class="o">=</span> <span class="n">clip_eta</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">)</span>
    <span class="n">adv_x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">eta</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">adv_x</span> <span class="o">=</span> <span class="n">utils_tf</span><span class="o">.</span><span class="n">clip_by_value</span><span class="p">(</span><span class="n">adv_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">)</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_target</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_target</span>
      <span class="n">targeted</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span>
      <span class="n">targeted</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">model_preds</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_probs</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
      <span class="n">preds_max</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_max</span><span class="p">(</span><span class="n">model_preds</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
      <span class="n">y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">to_float</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">model_preds</span><span class="p">,</span> <span class="n">preds_max</span><span class="p">))</span>
      <span class="n">y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">stop_gradient</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
      <span class="n">targeted</span> <span class="o">=</span> <span class="kc">False</span>
      <span class="k">del</span> <span class="n">model_preds</span>

    <span class="n">y_kwarg</span> <span class="o">=</span> <span class="s1">&#39;y_target&#39;</span> <span class="k">if</span> <span class="n">targeted</span> <span class="k">else</span> <span class="s1">&#39;y&#39;</span>

    <span class="n">fgm_params</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;eps&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps_iter</span><span class="p">,</span>
        <span class="n">y_kwarg</span><span class="p">:</span> <span class="n">y</span><span class="p">,</span>
        <span class="s1">&#39;ord&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span>
        <span class="s1">&#39;loss_fn&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss_fn</span><span class="p">,</span>
        <span class="s1">&#39;clip_min&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span>
        <span class="s1">&#39;clip_max&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">,</span>
        <span class="s1">&#39;clip_grad&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_grad</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
      <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;FGM is not a good inner loop step for PGD &quot;</span>
                                <span class="s2">&quot; when ord=1, because ord=1 FGM changes only &quot;</span>
                                <span class="s2">&quot; one pixel at a time. Use the SparseL1Descent &quot;</span>
                                <span class="s2">&quot; attack instead, which allows fine-grained &quot;</span>
                                <span class="s2">&quot; control over the sparsity of the gradient &quot;</span>
                                <span class="s2">&quot; updates.&quot;</span><span class="p">)</span>

    <span class="c1"># Use getattr() to avoid errors in eager execution attacks</span>
    <span class="n">FGM</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">FGM_CLASS</span><span class="p">(</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
        <span class="n">sess</span><span class="o">=</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;sess&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
        <span class="n">dtypestr</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dtypestr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">cond</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">_</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;Iterate until requested number of iterations is completed&quot;&quot;&quot;</span>
      <span class="k">return</span> <span class="n">tf</span><span class="o">.</span><span class="n">less</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">body</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">adv_x</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;Do a projected gradient step&quot;&quot;&quot;</span>
      <span class="n">adv_x</span> <span class="o">=</span> <span class="n">FGM</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">adv_x</span><span class="p">,</span> <span class="o">**</span><span class="n">fgm_params</span><span class="p">)</span>

      <span class="c1"># Clipping perturbation eta to self.ord norm ball</span>
      <span class="n">eta</span> <span class="o">=</span> <span class="n">adv_x</span> <span class="o">-</span> <span class="n">x</span>
      <span class="n">eta</span> <span class="o">=</span> <span class="n">clip_eta</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">)</span>
      <span class="n">adv_x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">eta</span>

      <span class="c1"># Redo the clipping.</span>
      <span class="c1"># FGM already did it, but subtracting and re-adding eta can add some</span>
      <span class="c1"># small numerical error.</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">adv_x</span> <span class="o">=</span> <span class="n">utils_tf</span><span class="o">.</span><span class="n">clip_by_value</span><span class="p">(</span><span class="n">adv_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">)</span>

      <span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">adv_x</span>

    <span class="n">_</span><span class="p">,</span> <span class="n">adv_x</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">while_loop</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">body</span><span class="p">,</span> <span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">zeros</span><span class="p">([]),</span> <span class="n">adv_x</span><span class="p">),</span> <span class="n">back_prop</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                             <span class="n">maximum_iterations</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span><span class="p">)</span>

    <span class="c1"># Asserts run only on CPU.</span>
    <span class="c1"># When multi-GPU eval code tries to force all PGD ops onto GPU, this</span>
    <span class="c1"># can cause an error.</span>
    <span class="n">common_dtype</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">float32</span>
    <span class="n">asserts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">utils_tf</span><span class="o">.</span><span class="n">assert_less_equal</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">eps_iter</span><span class="p">,</span>
                                                      <span class="n">dtype</span><span class="o">=</span><span class="n">common_dtype</span><span class="p">),</span>
                                              <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">common_dtype</span><span class="p">)))</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="c1"># The 1e-6 is needed to compensate for numerical error.</span>
      <span class="c1"># Without the 1e-6 this fails when e.g. eps=.2, clip_min=.5,</span>
      <span class="c1"># clip_max=.7</span>
      <span class="n">asserts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">utils_tf</span><span class="o">.</span><span class="n">assert_less_equal</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">),</span>
                                                <span class="mf">1e-6</span> <span class="o">+</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">,</span>
                                                               <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
                                                <span class="o">-</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span>
                                                          <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">)))</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">sanity_checks</span><span class="p">:</span>
      <span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">control_dependencies</span><span class="p">(</span><span class="n">asserts</span><span class="p">):</span>
        <span class="n">adv_x</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">identity</span><span class="p">(</span><span class="n">adv_x</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">adv_x</span></div>

<div class="viewcode-block" id="ProjectedGradientDescent.parse_params"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.ProjectedGradientDescent.parse_params">[docs]</a>  <span class="k">def</span> <span class="nf">parse_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                   <span class="n">eps</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span>
                   <span class="n">eps_iter</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span>
                   <span class="n">nb_iter</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
                   <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="nb">ord</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span>
                   <span class="n">loss_fn</span><span class="o">=</span><span class="n">softmax_cross_entropy_with_logits</span><span class="p">,</span>
                   <span class="n">clip_min</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">clip_max</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">y_target</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">rand_init</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">rand_init_eps</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">clip_grad</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                   <span class="n">sanity_checks</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                   <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Take in a dictionary of parameters and applies attack-specific checks</span>
<span class="sd">    before saving them as attributes.</span>

<span class="sd">    Attack-specific parameters:</span>

<span class="sd">    :param eps: (optional float) maximum distortion of adversarial example</span>
<span class="sd">                compared to original input</span>
<span class="sd">    :param eps_iter: (optional float) step size for each attack iteration</span>
<span class="sd">    :param nb_iter: (optional int) Number of attack iterations.</span>
<span class="sd">    :param y: (optional) A tensor with the true labels.</span>
<span class="sd">    :param y_target: (optional) A tensor with the labels to target. Leave</span>
<span class="sd">                     y_target=None if y is also set. Labels should be</span>
<span class="sd">                     one-hot-encoded.</span>
<span class="sd">    :param ord: (optional) Order of the norm (mimics Numpy).</span>
<span class="sd">                Possible values: np.inf, 1 or 2.</span>
<span class="sd">    :param loss_fn: Loss function that takes (labels, logits) as arguments and returns loss</span>
<span class="sd">    :param clip_min: (optional float) Minimum input component value</span>
<span class="sd">    :param clip_max: (optional float) Maximum input component value</span>
<span class="sd">    :param rand_init: (optional) Start the gradient descent from a point chosen</span>
<span class="sd">                      uniformly at random in the norm ball of radius</span>
<span class="sd">                      rand_init_eps</span>
<span class="sd">    :param rand_init_eps: (optional float) size of the norm ball from which</span>
<span class="sd">                          the initial starting point is chosen. Defaults to eps</span>
<span class="sd">    :param clip_grad: (optional bool) Ignore gradient components at positions</span>
<span class="sd">                      where the input is already at the boundary of the domain,</span>
<span class="sd">                      and the update step will get clipped out.</span>
<span class="sd">    :param sanity_checks: bool Insert tf asserts checking values</span>
<span class="sd">        (Some tests need to run with no sanity checks because the</span>
<span class="sd">         tests intentionally configure the attack strangely)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Save attack-specific parameters</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">eps</span> <span class="o">=</span> <span class="n">eps</span>
    <span class="k">if</span> <span class="n">rand_init</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">rand_init</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">default_rand_init</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">rand_init</span> <span class="o">=</span> <span class="n">rand_init</span>
    <span class="k">if</span> <span class="n">rand_init_eps</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">rand_init_eps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">rand_init_eps</span> <span class="o">=</span> <span class="n">rand_init_eps</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">eps_iter</span> <span class="o">=</span> <span class="n">eps_iter</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span> <span class="o">=</span> <span class="n">nb_iter</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">y_target</span> <span class="o">=</span> <span class="n">y_target</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="o">=</span> <span class="nb">ord</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">loss_fn</span> <span class="o">=</span> <span class="n">loss_fn</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="o">=</span> <span class="n">clip_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="o">=</span> <span class="n">clip_max</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_grad</span> <span class="o">=</span> <span class="n">clip_grad</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">eps</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">eps_iter</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
      <span class="c1"># If these are both known at compile time, we can check before anything</span>
      <span class="c1"># is run. If they are tf, we can&#39;t check them yet.</span>
      <span class="k">assert</span> <span class="n">eps_iter</span> <span class="o">&lt;=</span> <span class="n">eps</span><span class="p">,</span> <span class="p">(</span><span class="n">eps_iter</span><span class="p">,</span> <span class="n">eps</span><span class="p">)</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_target</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Must not set both y and y_target&quot;</span><span class="p">)</span>
    <span class="c1"># Check if order of the norm is acceptable given current implementation</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]:</span>
      <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Norm order must be either np.inf, 1, or 2.&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_grad</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
      <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Must set clip_min and clip_max if clip_grad is set&quot;</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">sanity_checks</span> <span class="o">=</span> <span class="n">sanity_checks</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;kwargs is unused and will be removed on or after &quot;</span>
                    <span class="s2">&quot;2019-04-26.&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="kc">True</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">CleverHans</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../source/attacks.html"><cite>attacks</cite> module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../source/model.html"><cite>model</cite> module</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>


  </body>
</html>